package com.org.khtn.gui;

import java.awt.BorderLayout;
import java.util.Map;
import java.util.Vector;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumn;
import javax.swing.tree.TreePath;

import com.org.khtn.data.Manager;

public class TableTab extends JPanel {
	public final static int ROW_HEADER = 0;
	public final static int COLUMN_NAME = 1;
	public final static int DATA_TYPE = 2;
	public JTable table;
	public static int number = 0;
	//public Vector listCellRenderer;
	public String databaseName;
	public boolean isSave;
	public String tableName;
	public TreePath selectionPath;
	public Vector primaryKeys;
	
	public TableTab(Content_TableDesignPopupMenu tableDesignPopupMenu, String dbName, String tbName, TreePath path) {
		setLayout(new BorderLayout());
		primaryKeys =new Vector();
		databaseName = dbName;
		tableName = tbName;
		isSave = false;
		selectionPath = path;
		//listCellRenderer = new Vector();
		table = new JTable();
		table.setModel(new MyTableDesign(table));
		table.setFillsViewportHeight(false);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		table.setRowHeight(20);
		table.setComponentPopupMenu(tableDesignPopupMenu);
		JScrollPane js = new JScrollPane(table);
		add(js);
		
		setUpDataTypeColumn(table.getColumnModel().getColumn(DATA_TYPE));
		setUpColumnName(table.getColumnModel().getColumn(COLUMN_NAME));
		setUpRowHeader(table.getColumnModel().getColumn(ROW_HEADER),primaryKeys);
		
		Vector v = new Vector();
		v.add("");
		v.add("");
		v.add("int");
		((MyTableDesign) table.getModel()).insertRow(v);
	}
	
	/*public TableTab(Map<String, String> m) {
		setLayout(new BorderLayout());
		
		table = new JTable();
		table.setModel(new MyTableView(table));
		table.setFillsViewportHeight(false);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setRowHeight(20);
		JScrollPane js = new JScrollPane(table);
		add(js);
		setUpColumnName(table.getColumnModel().getColumn(COLUMN_NAME));
		setUpRowHeader(table.getColumnModel().getColumn(ROW_HEADER));
		
		for (Map.Entry<String, String> e : m.entrySet()) {
			Vector v = new Vector();
			v.add("");
			v.add(e.getKey());
			v.add(e.getValue());
			((MyTableView) table.getModel()).insertRow(v);
		}
	}*/
	
	
	public TableTab(Map<String, String> m, Content_TableDesignPopupMenu tableDesignPopupMenu, String dbName, String tbName, TreePath path, Manager manager, Vector<String> keys) {
		setLayout(new BorderLayout());
		databaseName = dbName;
		isSave = true;
		tableName = tbName;
		selectionPath = path;
		table = new JTable();
		table.setModel(new MyTableDesign(table));
		table.setFillsViewportHeight(false);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		table.setRowHeight(20);
		table.setComponentPopupMenu(tableDesignPopupMenu);
		primaryKeys = new Vector();
		JScrollPane js = new JScrollPane(table);
		add(js);
		setUpDataTypeColumn(table.getColumnModel().getColumn(DATA_TYPE));
		setUpColumnName(table.getColumnModel().getColumn(COLUMN_NAME));
		setUpRowHeader(table.getColumnModel().getColumn(ROW_HEADER),primaryKeys);
		
		for (Map.Entry<String, String> e : m.entrySet()) {
			Vector v = new Vector();
			v.add("");
			v.add(e.getKey());
			v.add(e.getValue());
			((MyTableDesign) table.getModel()).insertRow(v);
		}
		table.addPropertyChangeListener(new TableDesignListener(table, manager,dbName, tbName));
		MyTableDesign model = (MyTableDesign) table.getModel();
		
		if (keys != null) {
			for (int i=0; i<keys.size(); i++) {
				primaryKeys.add(model.getColumnHasName(keys.elementAt(i)));
			}
		}
	}
	
	
	/*public TableTab(Map<String, Map<String, String>> record, Map<String, String> field, Content_TableSetValuePopupMenu tableSetValuePopupMenu,String dbName, String tbName, TreePath path) {
		setLayout(new BorderLayout());
		databaseName = dbName;
		isSave = true;
		tableName = tbName;
		selectionPath = path;
		table = new JTable();
		table.setModel(new MyTableSetValue(record, field));
		table.setFillsViewportHeight(false);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setRowHeight(20);
		table.setComponentPopupMenu(tableSetValuePopupMenu);
		JScrollPane js = new JScrollPane(table);
		add(js);
	}*/
	
	public TableTab(Map<String, Map<String, String>> record, Map<String, String> field, Content_TableSetValuePopupMenu tableSetValuePopupMenu,String dbName, String tbName, TreePath path, Vector<String> keys) {
		setLayout(new BorderLayout());
		databaseName = dbName;
		isSave = true;
		tableName = tbName;
		selectionPath = path;
		table = new JTable();
		table.setModel(new MyTableSetValue(record, field,table));
		table.setFillsViewportHeight(false);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setRowHeight(20);
		table.setComponentPopupMenu(tableSetValuePopupMenu);
		JScrollPane js = new JScrollPane(table);
		add(js);
		
		setUpRowViewHeader(table.getColumnModel().getColumn(ROW_HEADER));
		primaryKeys = new Vector();
		MyTableSetValue model = (MyTableSetValue) table.getModel();
		if (keys != null) {
			primaryKeys = keys;
		}
	}

	public void setUpDataTypeColumn(TableColumn dataTypeColumn) {
		JComboBox comboBox = new JComboBox();
		comboBox.addItem("int");
		comboBox.addItem("string");
		dataTypeColumn.setCellEditor(new DefaultCellEditor(comboBox));
		dataTypeColumn.setCellRenderer(new DataTypeRenderer());
		dataTypeColumn.setPreferredWidth(100);
	}
	
	public void setUpColumnName(TableColumn columnName) {
		columnName.setPreferredWidth(150);
	}
	
	public void setUpRowHeader(TableColumn rowHeaderColumn, Vector keys) {
		RowHeaderRenderer rowHeader = new RowHeaderRenderer(keys);
		rowHeaderColumn.setCellRenderer(rowHeader);
		rowHeaderColumn.setPreferredWidth(20);
		rowHeaderColumn.setResizable(false);
	}
	
	public void setUpRowViewHeader(TableColumn rowHeaderColumn) {
		RowViewRenderer rowHeader = new RowViewRenderer();
		rowHeaderColumn.setCellRenderer(rowHeader);
		rowHeaderColumn.setPreferredWidth(20);
		rowHeaderColumn.setResizable(false);
	}
}
